/*
 * Tasks.c
 *
 *  Created on: 2017-12-18
 *      Author: root
 */
#include "sys.h"
#include <pthread.h>
#include "tools.h"
#include <sys/shm.h>
#include <sys/ipc.h>
#include <string.h>
#include <stdlib.h>
#include "program.h"
#include <unistd.h>
#include <signal.h>
#include "app_debug_main.h"
#include "protocol_can.h"
#include "protocol_scan.h"
#include <fcntl.h>
#include <semaphore.h>
#include "database.h"
#include "ffs.h"

#include "can_task.h"
#include <stdio.h>
#include "tools.h"
#include "dca700.h"
#include "sys_main.h"
#include "meter_com.h"
#include "with_ui.h"
#include "program.h"
#include "include.h"
#ifdef XJ_MQTT_CLIENT
#include "meter_xj.h"
#include "interface_xj.h"
#endif
#ifdef NW_CLIENT
#include "meter_nw.h"
#include "pro_104.h"
#endif
#ifdef ORDERLY_CHARGE
#include "Interface_dllfk.h"
#endif

#ifdef QL_CLOUD_CLIENT
#include "meter_ql.h"
#include "interface_ql.h"
#endif

#include "log_main.h"
#include "database.h"
int g_shmid = -1;
//char _tmp_print_str[128];
void killexit(int signo)
{
	shmdt(g_uidata);
//	shmctl(g_shmid, IPC_RMID, 0);
	shmctl(g_shmid, IPC_RMID, 0);
	printf("  程序终止  ！！\n");
	exit(1);
}
void MainDataShow(u8 i)
{

	ui_debug("can是否正常  g_uidata->can_state[%d]=%d g_candata.errcode[%d]=%d",i,g_uidata->can_state[i],i,g_candata.errcode[i]);
    ui_debug("上次停止充电的原因  =%d  g_stop_reason=%d",g_uidata->gunmsg[i].stopreason,g_stop_reason[i]);
	ui_debug("当前是否有电表故障  g_uidata->gunmsg[%d].meter_state=%d",i,g_uidata->gunmsg[i].meter_state);
	ui_debug("是否APP充电  g_uidata->cmd3[0].state=%d g_ifApp[%d]=%d",g_uidata->cmd3[0].state,i,g_ifApp[i]);
	ui_debug("打印枪连接状态  conn_state[%d] =%d ", i, g_uidata->gunmsg[i].gun_conn_state);

//	sprintf(_tmp_print_str," 打印枪连接状态  conn_state[%d] =%d \n ", i,
//			g_uidata->gunmsg[i].gun_conn_state);
//	fs_debug_104(_tmp_print_str);

	ui_debug("控制器故障[%d]=%d", i,g_uidata->gunmsg[i].errcode[1]);//[0],g_uidata->gunmsg[i].errcode[1]);

	ui_debug("打印系统当前状态  gm_sys_state[%d] =%d  g_uidata->devtype=%d  g_uidata->gunmsg[i].charge_state=%d",
			i, gm_sys_state[i], g_uidata->devtype,g_uidata->gunmsg[i].charge_state);
	ui_debug("打印充电条件：g_uidata->cmd2[%d].check_result=%d   g_uidata->cmd2[%d].state =%d SOC=%d,ycSOC=%d",
			i, g_uidata->cmd2[i].check_result, i, g_uidata->cmd2[i].state,g_uidata->gunmsg[i].u2.dc.MX[2],g_candata.gun[i].yc_data.dc.MX[2]);
	ui_debug("g_uidata->cmd2[%d].result=%d   g_uidata->gunmsg[%d].charge_state =%d,g_uidata->net_state=%d",
			i, g_uidata->cmd2[i].result, i,g_uidata->gunmsg[i].charge_state, g_uidata->net_state);
	ui_debug("g_uidata->cmd4[i].req_state=%d", g_uidata->cmd4[i].req_state);
	ui_debug("充电停止码 %c  %c %c %c ",g_uidata->cmd3[i].stopcode[0],g_uidata->cmd3[i].stopcode[1],g_uidata->cmd3[i].stopcode[2],g_uidata->cmd3[i].stopcode[3]);


}
void finStopCharge_down(u8 gunid)
{
	static end_mx end;
	end.MX1=0xF2;
	end.MX2=0;
     g_stop_reason[gunid]=9;
     
	printf("TCU电源中断停止充电\r\n");
	FinishStopCharge(gunid,(void*)&end);
}


/*********************************************************************************************************
 *                                      APP MAIN TASK
 *********************************************************************************************************/
int os_main_task(void)
{
	//protocol_can_init
	int ret;
	key_t uikey;
	int i=0;
	int TCU_Type=0;

	TDateTime tm_now,EndDay;

	u8 guntab4[]={0,0,0,0,0,0,0,0};
	u8 guntab2[]={0,0,0,0,0,0,0,0};
	u8 guntab5[]={0,0,0,0,0,0,0,0};
	sem_t  *m2ui_sem;//*ui2m_sem,

	system("echo  1  >   /proc/canfilter/switch");
	EndDay.Day=15;
	EndDay.Month=4;
	EndDay.Year=2020;
	read_current_datetime(&tm_now);

//  system("ifconfig eth0 192.168.1.221");
//
//	ui_debug1("收到：len=%d ",sizeof(xxxx));
//	ui_debug1(" buf=%.*a",504,xxxx);
	TCU_Type=protocol_platform_check();


    printf("测试得到硬件版本=%d\n",TCU_Type);//上传事件  err_no=
    FS_Init();

#ifdef OLD_CONF
	pconf1 conf1;
	printf("111新建立的文件读取！！！\r\n");
	ret=ReadConf1((void*)&conf1);

	printf("111日志开始记录日期  year=%d  month=%d  day=%d  天数=%d \n",conf1.StartLogDay.Year,conf1.StartLogDay.Month,conf1.StartLogDay.Day,conf1.logdays);
	if(ret!=0)
	{
		printf("新建立的文件读取！！！ret=%d\r\n",ret);
		ret=ReadConf1((void*)&conf1);

		if(ret!=0)
			ReadConf1((void*)&conf1);
	}

		pconfig->firm_id=conf1.firm_id;//公司编号
		pconfig->devtype=conf1.devtype;
		pconfig->gunnum=conf1.gunnum;
		memcpy(pconfig->server_ip,conf1.server_ip,32);
		pconfig->server_port=conf1.server_port;
		memcpy(pconfig->devNO,conf1.devNO,32);
		memcpy(pconfig->propertyCode,conf1.propertyCode,64);
		memcpy(pconfig->can_ver,conf1.can_ver,32);
		pconfig->MaxV=conf1.MaxV;
		pconfig->MaxI=conf1.MaxI;


		pconfig->INMT_baud=conf1.FMt_baud;

		//printf("pconfig->INMT_baud=conf1.FMt_baud   pconfig->INMT_baud=%d   conf1.FMt_baud=%d",pconfig->INMT_baud,conf1.FMt_baud);
		pconfig->OUTMT_baud=conf1.FMt_baud;
		pconfig->cannum=conf1.cannum;
		pconfig->hardware_ver=conf1.hardware_ver;

		pconfig->ctrlpw_type=conf1.ctrlpw_type;
		pconfig->ShowStopReason=conf1.ShowStopReason;
		pconfig->iflog=conf1.iflog;//是否将协议日志记录到flash中


		pconfig->StartLogDay=conf1.StartLogDay;
		pconfig->logdays=conf1.logdays;
		printf("日志开始记录日期  year=%d  month=%d  day=%d\n",conf1.StartLogDay.Year,conf1.StartLogDay.Month,conf1.StartLogDay.Day);
		printf("日志开始记录日期  year=%d  month=%d  day=%d\n",pconfig->StartLogDay.Year,pconfig->StartLogDay.Month,pconfig->StartLogDay.Day);
#ifdef 	XJ_MQTT_CLIENT
		pconfig->mt_ct=conf1.mt_ct;//互感器变比
#endif

#else
	ret=ReadConf1((void*)pconfig);
	if(ret!=0)
	{
		printf("新建立的文件读取！！！\r\n");
		ret=ReadConf1((void*)pconfig);
		if(ret!=0)
			ReadConf1((void*)pconfig);
	}

#endif
	sprintf(_tmp_print_str,"测试得到硬件版本=%d\n",TCU_Type);
	fs_debug_104(_tmp_print_str);

#ifdef 	XJ_MQTT_CLIENT
    read_current_datetime(&tm_now);
	if(TCU_Type < 0)
	{
		printf("测试到硬件版本错误 =%d\n",TCU_Type);
		sprintf(_tmp_print_str,"测试到硬件版本错误 =%d\n",TCU_Type);
		fs_debug_104(_tmp_print_str);
		exit(1);
	}
#else
    #ifdef QL_CLOUD_CLIENT
        read_current_datetime(&tm_now);
        // 这里暂时写死，是 DAC220
        TCU_Type = DCA220;
        pconfig->hardware_ver = TCU_Type;
    	if(TCU_Type < 0)
    	{
    		printf("测试到硬件版本错误 =%d\n",TCU_Type);
    		sprintf(_tmp_print_str,"测试到硬件版本错误 =%d\n",TCU_Type);
    		fs_debug_104(_tmp_print_str);
    		exit(1);
    	}
    #else
    	if(TCU_Type<0)
            TCU_Type=2;
    	if(TCU_Type>0)
        	pconfig->hardware_ver = TCU_Type;
    #endif 
#endif

    if(TCU_Type==DCA700 || TCU_Type==DCA600)
    {
    	pconfig->mt_portnum = 4;
    }
    else
    {
    	pconfig->mt_portnum = 2;
    }

#ifdef CHECK_POWER_DOWN

	int flag_down = 0;
	int checkPwEn = 1;
	int ncheck=0;
	if (cpld_mmap_init() < 0) {
		printf("----CPLD memory init failed!\n");
	} else {
		printf("----CPLD memory init succ!!!!\n");
	}
    
	while (1)
	{
		if (check_power_12v(pconfig->hardware_ver) > 0)
		{
			flag_down = 0;
			checkPwEn=1;
			printf("----System Power is ON!\n");
			break;
		}
		else
		{
			ncheck++;
			if (ncheck >= 3)
			{
				flag_down = 0;
				checkPwEn = 0;
				printf("----此时系统不可能没电，认为超级电容损坏\n");
				break;
			}
		}
	}
#endif//CHECK_POWER_DOWN   fail to establish tcp

	printf("数据库注册，标注各表的类型 0成功 1失败\n\n");
    registerdb();
    printf("pconfig->INMT_baud=%d  pconfig->OUTMT_baud=%d \r\n",(int)pconfig->INMT_baud,(int)pconfig->OUTMT_baud);
    initmeter((void*)pconfig);

	signal(SIGPIPE, SIG_IGN);
	signal(SIGINT, killexit);
	//共享内存初始化
	uikey = ftok("/etc/hostname", 2);
	g_shmid = shmget(uikey, sizeof(chargpile_data), IPC_CREAT | SHM_R | SHM_W);//IPC_PRIVATE
//	g_shmid = shmget(0x5A5A5A5A, sizeof(chargpile_data), IPC_CREAT | SHM_R | SHM_W);//IPC_PRIVATE
	if (g_shmid == -1) {
		printf("shmget error!\n");
		system("reboot");
	//	return -1;
	}

	g_uidata = (chargpile_data *) shmat(g_shmid, NULL, 0);
	if (g_uidata == (void*) -1) {
		main_debug("shmat error!\n");
		return -1;
	}
    
	m2ui_sem = sem_open("maintoui", O_CREAT, 0644, 0);
	sem_post(m2ui_sem);
	memset(g_uidata, 0, sizeof(chargpile_data));
	printf("共享内存Key   share memory Key : 0X%X\n", uikey);
	printf("共享内存ID    share memory ID: %d\n", g_shmid);
	printf("共享内存地址   share memory addr : %x\n", g_uidata);
    printf("共享内存大小   share memory size : %d\n", sizeof(chargpile_data));
//	initTaskUIServer();
    sleep(1);
#ifdef ORDERLY_CHARGE
    fknetInit(*pconfig);
#endif

    printf("wwwwwwwwwwwwwwwwwwwwfffkkk\r\n");
	initUI();
    printf("111wwwwwwwwwwwwwwwwwwwwfffkkk\r\n");
    initTaskTcuRecv();
    printf("222wwwwwwwwwwwwwwwwwwwwfffkkk\r\n");
    
#ifdef XJ_MQTT_CLIENT
    XiaoJuInit(*pconfig);
#endif

// 青莲云接口初始化 
#ifdef QL_CLOUD_CLIENT
    QLcloudInit(*pconfig);
#endif

#ifdef  NW_CLIENT
    netInit(*pconfig);
#endif
     //  initTaskClient();

   for(i=0;i<pconfig->gunnum;i++)
	{
		g_uidata->cmd3[i].state=0;
		g_uidata->cmd3[i].guntab=0;
		g_uidata->cmd3[i].stopcode[0]='0';
		g_uidata->cmd3[i].stopcode[1]='0';
		g_uidata->cmd3[i].stopcode[2]='0';
		g_uidata->cmd3[i].stopcode[3]='0';
		g_uidata->cmd3[i].stopcode[4]=0x00;

		g_stop_reason[i]=0;
		g_uidata->gunmsg[i].m_stopReason=0;
		meter_err_state[i]=0;
	}
    
	while (1) 
    {
	    ui_debug("打印管理的枪数量  gunnum=%d",g_uidata->gunnum);
    
        for(i=0;i<pconfig->gunnum;i++)
        {
            // 更新界面枪数据 
	    	updateStatus2UI(i,g_uidata);
        }
        
		sleep(1);
        
#ifdef CHECK_POWER_DOWN
		if (checkPwEn == 1) {
			if (check_power_12v(pconfig->hardware_ver) > 0) {
				flag_down = 0;
				printf("----System Power is ON!\n");
			} else {
				flag_down = 1;
				printf("----System Power is OFF!\n");
			}
		}
        
		if(flag_down==1 )
		{

		       for(i=0;i<pconfig->gunnum;i++)
		       {
		    	   	  if(gm_sys_state[i]==_SYS_START_FIN_)
			                finStopCharge_down(i);
			}
		}
#endif

		for (i = 0; i < pconfig->gunnum; i++) 
        {
			MainDataShow(i);
            
			if(pconfig->gunnum==1)
			{
				guntab2[i]=0;
				guntab4[i]=0;
				guntab5[i]=0;
			}
			else
			{
				guntab2[i]= g_uidata->cmd2[i].guntab-1;//充电桩当前状态charge_stat
				guntab4[i]= g_uidata->cmd4[i].guntab-1;
				guntab5[i]= g_uidata->cmd5[i].guntab-1;
			}
            
//			g_uidata->cmd2[i].yct_car_para.TAC
			 if (g_uidata->cmd2[i].check_result == 1
					&& g_uidata->cmd2[i].state == 1
					&& guntab2[i]==i
					&& gm_sys_state[i] == _SYS_READY_)
			 {
				int ret1;

				g_uidata->cmd2[i].check_result=0;
				g_uidata->cmd2[i].state=0;
				g_ifApp[i]=_CARD_MODE_;
				g_uidata->cmd3[i].state=_CARD_MODE_;
				gm_ctrl_state[i]=_CTRL_NORMAL_;
				//////////////////////////////////////
#ifdef CTRL_PW_ENABLE
		//		g_uidata->cmd2[i].ctrlMode=_CTRL_COMB_;

				printf("g_uidata->cmd2[i].ctrlMode=%d\r\n",g_uidata->cmd2[i].ctrlMode);
				ret1 = TCUStartChargeProcess(i,g_uidata->cmd2[i].ctrlMode);
#else
				ret1 = TCUStartChargeProcess(i,0);//_CTRL_COMB_  0 不是共充
#endif
				if (ret1 != 0) 
                {
					ui_debug("接收到UI发来的充电要求,但充电失败！！！ret1=%d", ret1);
					g_uidata->cmd2[i].fail_Reason = ret1;
					g_uidata->cmd2[i].result = 2;
					g_stop_reason[i] = 5;
					g_stop_detail[i] = ret1;
			    } 
                else 
                {
					TDateTime datetime;
					u16 b1,b2;
					read_current_datetime(&datetime);
	                b1=(datetime.Hour+8+datetime.Sec)%100;
	                b2=(datetime.Min+datetime.Sec)%100;
					g_stop_reason[i]=0;
					g_sss[i]=0;
					g_uidata->cmd2[i].result = 1;
					gm_sys_state[i] = _SYS_WAIT_CHARGING_START_;
					g_uidata->gunmsg[i].charge_state=1;

	////////////////////////////////////////////////////////////////////////////////////////
#ifdef  NW_CLIENT
					int j=0;
				    char buf[8];
	//				g_ser_num++;
					g_chargepara[i].value = g_uidata->cmd2[i].value; //用户设置参数，根据用户设置充电方式，可能为充电金额、充电时间、充电电量
					g_chargepara[i].billmode = g_uidata->cmd2[i].mod;
					g_chargepara[i].account_balance = g_uidata->cmd2[i].available_money; //g_uidata->cmd2[i].available_money;
					g_chargepara[i].startmode = 0; //充电卡充电
#endif

#ifdef YCC_CLIENT
					memcpy((u8*)&yct_car_para[i],(u8*)&g_uidata->cmd2[i].yct_car_para,sizeof(yct_rf_consum_card));
					g_chargepara[i].ppara_append=(void*)&yct_car_para[i];

					printf("充电参数  gunid=%d  value=%d\r\n",i,g_chargepara[i].value);
					memset(g_chargepara[i].userId, 0xff, 8);
					strBcd2BcdArray(g_uidata->cmd2[i].cardNUM,(char*) &g_chargepara[i].cardId[0]);
				    datarecording(i, 1);

                    sprintf((char*)g_chargepara[i].ser_num,"card%d%d%d%d%d%d%d%d",b1,b2,datetime.Sec,datetime.Min,datetime.Hour,datetime.Day,datetime.Month,datetime.Year);
                    g_chargepara[i].guntab =guntab2[i];
					g_chargepara[i].ser_num[0] = byte2bcd(b1);
					g_chargepara[i].ser_num[1] = byte2bcd(b2);
					g_chargepara[i].ser_num[2] = byte2bcd(datetime.Sec);
					g_chargepara[i].ser_num[3] = byte2bcd(datetime.Min);
					g_chargepara[i].ser_num[4] = byte2bcd(datetime.Hour);
					g_chargepara[i].ser_num[5] = byte2bcd(datetime.Day);
					g_chargepara[i].ser_num[6] = byte2bcd(datetime.Month);
					g_chargepara[i].ser_num[7] = byte2bcd(datetime.Year - 2000);//g_uidata->cmd3

					pconfig->devNO[16] = '\0';
					strBcd2BcdArray(pconfig->devNO, buf);
					for (j = 0; j < 8; j++)
						g_chargepara[i].ser_num[8 + j] = buf[7 - j];
					//saveChargeRd(g_ser_num);


#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef XJ_MQTT_CLIENT

				//	char buf[8];
	//				g_ser_num++;
					g_charge_arg[i].value = g_uidata->cmd2[i].value; //用户设置参数，根据用户设置充电方式，可能为充电金额、充电时间、充电电量
					g_charge_arg[i].billmode = g_uidata->cmd2[i].mod;
					g_charge_arg[i].account_balance = 	g_uidata->cmd2[i].available_money; //g_uidata->cmd2[i].available_money;
					g_charge_arg[i].startmode = 0; //充电卡充电
//					memset(g_charge_arg[i].userId, 0xff, 8);
//					strBcd2BcdArray(g_uidata->cmd2[i].cardNUM,(char*) &g_charge_arg[i].cardId[0]);

				   datarecording(i, 1);
                   sprintf((char*)g_charge_arg[i].serialnum,"card%d%d%d%d%d%d%d%d",b1,b2,datetime.Sec,datetime.Min,datetime.Hour,datetime.Day,datetime.Month,datetime.Year);
                   g_charge_arg[i].guntab =guntab2[i];
//				   g_charge_arg[i].serialnum[0] = byte2bcd(b1);
//				   g_charge_arg[i].serialnum[1] = byte2bcd(b2);
//				   g_charge_arg[i].serialnum[2] = byte2bcd(datetime.Sec);
//					g_charge_arg[i].serialnum[3] = byte2bcd(datetime.Min);
//					g_charge_arg[i].serialnum[4] = byte2bcd(datetime.Hour);
//					g_charge_arg[i].serialnum[5] = byte2bcd(datetime.Day);
//					g_charge_arg[i].serialnum[6] = byte2bcd(datetime.Month);
//					g_charge_arg[i].serialnum[7] = byte2bcd(datetime.Year - 2000);//g_uidata->cmd3

//					pconfig->devNO[16] = '\0';
//					strBcd2BcdArray(pconfig->devNO, buf);
//					for (j = 0; j < 8; j++)
//						g_chargepara[i].ser_num[8 + j] = buf[7 - j];
				//		        saveChargeRd(g_ser_num);

#endif

#ifdef QL_CLOUD_CLIENT
                    g_charge_arg[i].value    = g_uidata->cmd2[i].value; //用户设置参数，根据用户设置充电方式，可能为充电金额、充电时间、充电电量
                    g_charge_arg[i].billmode = g_uidata->cmd2[i].mod;
                    g_charge_arg[i].account_balance = g_uidata->cmd2[i].available_money; //g_uidata->cmd2[i].available_money;
                    g_charge_arg[i].startmode = 0; //充电卡充电
                    
                    datarecording(i, 1);
                    sprintf((char*)g_charge_arg[i].serialnum,"card%d%d%d%d%d%d%d%d",b1,b2,datetime.Sec,datetime.Min,datetime.Hour,datetime.Day,datetime.Month,datetime.Year);
					        g_charge_arg[i].guntab =guntab2[i];
#endif

#ifdef CTRL_PW_ENABLE
					if(pconfig->ctrlpw_type==_CTRL_COMB_EN_ && g_uidata->cmd2[i].ctrlMode==_CTRL_COMB_EN_  &&  gm_ctrl_state[i]==_CTRL_NORMAL_)
					{
					    gm_ctrl_state[i]=_CTRL_COMB_EN_;
					    ui_debug("接收到UI发来的充电要求(共充)");
					    //datarecording(1, 1);
					}
#endif
        			g_uidata->cmd2[i].fail_Reason = 0;
        		    g_uidata->cmd2[i].result = 0;
        			g_uidata->cmd3[i].state=0;//电表通信错误
        			card_err_state[i]=0;

				ui_debug("接收到UI发来的充电要求,控制器处于准备好状态，等待控制器动作！！！充电参数=%d   mode=%d   money=%d  枪号=%d  ",
						 g_uidata->cmd2[i].value, g_uidata->cmd2[i].mod,g_uidata->cmd2[i].available_money, i);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			    }
			} 
            else if (g_uidata->cmd4[i].req_state == 1 && guntab4[i]==i) 
            {
	//			if (g_uidata->cmd4[i].stopReason == 0)
				{
					printf("接收到UI发来的停止充电要求！！！g_uidata->cmd4[i].guntab=%d\r\n ",
							g_uidata->cmd4[i].guntab);
					printf("打印系统当前状态  gm_sys_state[%d] =%d  g_uidata->devtype=%d  g_uidata->gunmsg[i].charge_state=%d",
							i, gm_sys_state[i], g_uidata->devtype,g_uidata->gunmsg[i].charge_state);
					if (gm_sys_state[i] == _SYS_START_FIN_)
					{
						 if(g_ifApp[i]==_APP_MODE_)
							 TCUStopChargeProcess(i, 8);//停止原因 8
						 else
							 TCUStopChargeProcess(i, 1);//停止原因 1
					}
					else if (gm_sys_state[i] == _SYS_START_FAIL_)
					{
						gm_sys_state[i] =_SYS_STOP_FIN_;
					}
				}
//				else if(g_uidata->cmd4[i].stopReason == 1)
//				{
//					TCUStopChargeProcess(i,5);
//					card_err_state[i]=1;
//				}
				g_uidata->cmd2[i].result = 0;
			    //	1本地刷卡终止，2远程终止充电，3充满自动停止，4余额不足，5充电桩故障，6 BMS故障
			    //	7、急停按钮停止256 其他，
			}
            else if ((meter_err_state[i]==1 || g_candata.errcode[i] )
					//else if ((meter_err_state_xj[i]==1 || g_candata.errcode[i]  || (g_ifApp[i]==_APP_MODE_ && g_uidata->net_state==0))
					&& (gm_sys_state[i]==_SYS_START_FIN_ || gm_sys_state[i]==_SYS_WAIT_CHARGING_START_ ) )
            {// && gm_sys_state[i]==_SYS_START_FIN_
			 //	g_candata.errcode[gunid]
				printf("出现故障,停止充电！！！\n");
//              	sprintf(_tmp_print_str," TCU出现故障,停止充电  gunid=%d  meter_err_state[i] =%d g_candata.errcode[i]=%d\n ",i ,meter_err_state[i],g_candata.errcode[i]);
//              	fs_debug_104(_tmp_print_str);
				// TCUStopChargeProcess(i,5);// 2021-07-30 测试使用 zhouhao
			}
			else if (g_uidata->cmd5[i].req_state == 1 && guntab5[i]==i) 
            {
			    char   ctrltype;
			    int     ctrlvaule;

				ui_debug("调节功率  调节功率  调节功率  调节功率 ！！！gunid=%d, %d  %d",i,g_uidata->cmd5[i].ctrltype,g_uidata->cmd5[i].ctrlvaule);
				g_uidata->cmd5[i].req_state = 2;

				if(g_uidata->cmd5[i].ctrltype == 0)
				{
					ctrltype  = 2;//按百分比调节
					ctrlvaule = g_uidata->cmd5[i].ctrlvaule;
				}
				else if(g_uidata->cmd5[i].ctrltype == 1)
				{
					ctrltype = 1;//按绝对值调节
					ctrlvaule=g_uidata->cmd5[i].ctrlvaule*10;
				}
				else
				{
					ctrltype=g_uidata->cmd5[i].ctrltype;
					ctrlvaule=0;
				}
				orderFormOther(i,ctrltype,ctrlvaule,3600,NULL);//&bak_ctrl_pw);
				//can_adjust_charge_pw(i,ctrltype,ctrlvaule);
			}
			else
			{
				//printf("xxxxxxxxxxxxxxxxxxx  ssi=%d  gm_sys_state[i] =%d  g_uidata->gunmsg[i].charge_state=%d\r\n", ssi,gm_sys_state[i],g_uidata->gunmsg[i].charge_state);
			}
		}
	}
	return 0;
}
